package com.eight.qianTeacher.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.common.pojo.EduChapter;
import com.eight.qianTeacher.dao.EduChapterDao;
import com.eight.qianTeacher.service.EduChapterService;
import com.eight.qianTeacher.vo.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/6/6 20:44
 */
@Service
public class EduChapterServiceImpl implements EduChapterService {
    @Resource
    private EduChapterDao eduChapterDao;
    @Override
    public List<EduChapter> findAll(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(id)){
            wrapper.eq("course_id",id);
        }

        List<EduChapter> eduChapters = eduChapterDao.selectList(wrapper);
        return eduChapters;
    }

    @Override
    public List<EduChapter> insertChapter(EduChapter eduChapter) {

        if (StringUtils.hasText(eduChapter.getId())){
            eduChapterDao.updateById(eduChapter);
        }else {
            eduChapter.setId(IdUtil.getSnowflakeNextIdStr());
            eduChapter.setGmtCreate(LocalDateTime.now());
            eduChapter.setGmtModified(LocalDateTime.now());
            eduChapter.setTitle(eduChapter.getTitle());
            eduChapter.setSort(eduChapter.getSort());
            eduChapter.setCourseId(eduChapter.getCourseId());
            eduChapterDao.insert(eduChapter);
        }
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",eduChapter.getCourseId());
        return eduChapterDao.selectList(wrapper);
    }
}
